//
//  QLMBrandNameTypeVC.m
//  Xiaomakuaixiu
//
//  Created by 龚碧容 on 2017/11/17.
//  Copyright © 2017年 hwd. All rights reserved.
//

#import "QLMBrandNameTypeVC.h"

#import "QLMPeijianListCVCell.h"
#import "QLMCartypeCReView.h"

@interface QLMBrandNameTypeVC ()<UICollectionViewDelegate,UICollectionViewDataSource>{
    NSString *carTypeStr;
    NSString *carSeriStr;
    
    UIView *lineView;
    UILabel *rTipLabel;
    UILabel *label;
    
    QLMPeijianListCVCell *pjCollectionCell;
    QLMCartypeCReView *reusableview;
}

@property (nonatomic,strong) UICollectionView *pjCollectionView;
@property (nonatomic,strong) NSArray *firstLetterArray;

@end

@implementation QLMBrandNameTypeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _firstLetterArray=[NSMutableArray array];
    [self aCreatMainView];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden=YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)aCreatMainView{
    
    UIView *coverView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    coverView.backgroundColor = [UIColor blackColor];
    coverView.alpha = 0.3;
    coverView.userInteractionEnabled=YES;
    [self.view addSubview:coverView];
    
    _leftView = [[UIView alloc] init];
    _leftView.frame = CGRectMake(0,0,80,kScreen_height);
    _leftView.backgroundColor = [UIColor clearColor];
    _leftView.userInteractionEnabled=YES;
    [self.view addSubview:_leftView];
    
    UIView *alertView = [[UIView alloc] init];
    alertView.frame = CGRectMake(60,0,kScreen_width-80,kScreen_height);
    alertView.backgroundColor = [UIColor whiteColor];
    alertView.userInteractionEnabled=YES;
    [self.view addSubview:alertView];
    
    UIView *navView = [[UIView alloc] init];
    navView.frame = CGRectMake(60,0,kScreen_width-60,QLM_StatusBarAndNavigationBarHeight);
    navView.backgroundColor = kNavigationBarColor;
    [self.view addSubview:navView];
    
    _leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _leftButton.frame = CGRectMake(20, 20+IOS11_OR_LATER_SPACE(24), 44, 44);
    [_leftButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [_leftButton setBackgroundColor:[UIColor clearColor]];
    _leftButton.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
    [navView addSubview:_leftButton];
    
    UILabel *_titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(alertView.frame.size.width/2-30,20+IOS11_OR_LATER_SPACE(24),60, 44)];
    _titleLabel.backgroundColor = [UIColor clearColor];
    _titleLabel.font = [UIFont boldSystemFontOfSize:16.0f];
    _titleLabel.textColor = [UIColor whiteColor];
    _titleLabel.text=@"品牌";
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    [navView addSubview:_titleLabel];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
    layout.minimumInteritemSpacing = 0;// 垂直方向的间距
    layout.minimumLineSpacing = 0; // 水平方向的间距
    
    _pjCollectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(0, QLM_StatusBarAndNavigationBarHeight, alertView.bounds.size.width, kScreen_height-QLM_StatusBarAndNavigationBarHeight) collectionViewLayout:layout];
    _pjCollectionView.delegate=self;
    _pjCollectionView.dataSource=self;
    _pjCollectionView.showsVerticalScrollIndicator=NO;
    _pjCollectionView.backgroundColor=[UIColor whiteColor];
    _pjCollectionView.alwaysBounceVertical = YES;
    _pjCollectionView.collectionViewLayout=layout;
    [_pjCollectionView registerClass:[QLMPeijianListCVCell class] forCellWithReuseIdentifier:@"QLMPeijianListCVCellId"];
    [alertView addSubview:_pjCollectionView];
    
    [_pjCollectionView registerClass:[QLMCartypeCReView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UICollectionReusableViewId12"];
    
    lineView=[[UIView alloc] initWithFrame:CGRectMake(kScreen_width-20,QLM_StatusBarAndNavigationBarHeight,20,kScreen_height-QLM_StatusBarAndNavigationBarHeight)];
    lineView.backgroundColor=[UIColor whiteColor];
    lineView.userInteractionEnabled=YES;
    [self.view addSubview:lineView];
    
    rTipLabel=[[UILabel alloc] init];
    [rTipLabel setFrame:CGRectMake(0, 0, lineView.frame.size.width, lineView.frame.size.height)];
    rTipLabel.backgroundColor=[UIColor clearColor];
    rTipLabel.numberOfLines=0;
    rTipLabel.userInteractionEnabled=YES;
    rTipLabel.textColor=[UIColor blackColor];
    rTipLabel.font=[UIFont systemFontOfSize:15.0f];
    [lineView addSubview:rTipLabel];
    
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] init];
    [tap addTarget:self action:@selector(clickStarIndexView:)];
    [rTipLabel addGestureRecognizer:tap];
}

- (void)_creatAFirstLetterArray:(NSArray *)firstLetterArray{
    
    rTipLabel.frame=CGRectMake(0, kScreen_height/2-10 * firstLetterArray.count, 20,20 * firstLetterArray.count);
    
    NSString *indexesString = [firstLetterArray componentsJoinedByString:@"\n"];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.minimumLineHeight = 20;
    paragraphStyle.alignment = NSTextAlignmentCenter;
    NSAttributedString *attriString = [[NSAttributedString alloc] initWithString:indexesString   attributes:@{ NSParagraphStyleAttributeName : paragraphStyle, NSForegroundColorAttributeName:UIColorFromRGB(0x999999),NSBaselineOffsetAttributeName:@(1)}];
    rTipLabel.attributedText = attriString;
}

-(void)clickStarIndexView:(UITapGestureRecognizer*)tap{
    
    CGPoint location = [tap locationInView:rTipLabel];
    NSInteger index = location.y / 20 ;
    
    CGRect destinationRect = [_pjCollectionView layoutAttributesForSupplementaryElementOfKind:UICollectionElementKindSectionHeader atIndexPath:[NSIndexPath indexPathForRow:0 inSection:index]].frame;
    
    CGFloat offsetY = MIN(destinationRect.origin.y,_pjCollectionView.contentSize.height-_pjCollectionView.frame.size.height);
    [_pjCollectionView setContentOffset:CGPointMake(0, offsetY) animated:NO];
}


#pragma mark - UICollectionView datasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section==0) {
        return _hotBrandArr.count;
    }
    NSArray *tmpArray=_itemArray[section-1];
    return tmpArray.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    //按字母顺序排序
    _firstLetterArray = [[_brandDic allKeys] sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        return [obj1 compare:obj2 options:NSNumericSearch];
    }];
    [self _creatAFirstLetterArray:_firstLetterArray];
    
    return _firstLetterArray.count+1;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((kScreen_width-80-20)/3, 34);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifiew=@"QLMPeijianListCVCellId";
    pjCollectionCell=[collectionView dequeueReusableCellWithReuseIdentifier:identifiew forIndexPath:indexPath];
    
    if (indexPath.section==0) {
        if (_hotBrandArr.count!=0) {
            pjCollectionCell.pjnameLabel.text=[_hotBrandArr[indexPath.row] objectForKey:@"codeName"];
        }
    }else{
        if (_itemArray.count!=0) {
            if ([_itemArray[indexPath.section-1] count]!=0) {
                pjCollectionCell.pjnameLabel.text=[[_itemArray[indexPath.section-1] objectAtIndex:indexPath.row] objectForKey:@"codeName"];
            }
        }
    }
    
    return pjCollectionCell;
}

/** section的margin*/
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 5, 5, 5);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView
                   layout:(UICollectionViewLayout*)collectionViewLayout
minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 5;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView
                   layout:(UICollectionViewLayout*)collectionViewLayout
minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 5;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    if([kind isEqualToString:UICollectionElementKindSectionHeader]){
        
        reusableview= [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UICollectionReusableViewId12" forIndexPath:indexPath];
        if (indexPath.section==0) {
            reusableview.tiplabel.text =@"热门品牌";
        }else{
            reusableview.tiplabel.text = _firstLetterArray[indexPath.section-1];
        }
        return reusableview;
        
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(kScreen_width-80, 34);
}

#pragma mark -- UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary *brandDic=[NSDictionary dictionary];
    if (indexPath.section==0) {
        brandDic=_hotBrandArr[indexPath.row];
    }else{
        brandDic=[_itemArray[indexPath.section-1] objectAtIndex:indexPath.row];
    }
    
    [_delegate didselectItem:brandDic];
}

@end
